package com.example.news.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.news.pojo.News;
import com.example.news.pojo.NewsType;
import com.example.news.pojo.Result;
import com.example.news.service.NewsService;
import com.example.news.service.NewsTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/newsType")
@RequiredArgsConstructor
public class NewsTypeController {

    @Autowired
    private NewsTypeService newsTypeService;

    @PostMapping("/search")
    public Result searchType(@RequestBody News news,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        Page<NewsType> newsTypePage = newsTypeService.searchPageType(news, pageNum, pageSize);
        return Result.data(newsTypePage);
    }

    @GetMapping("/all")
    public List<NewsType> getAllType() {
        List<NewsType> allType = newsTypeService.list();
        return allType;
    }

    @PostMapping
    public Result save(@RequestBody NewsType newsType) {
        newsTypeService.saveNewType(newsType);
        return Result.ok("添加成功");
    }
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> ids) {
        newsTypeService.removeNewTypeByIds(ids);
        return   Result.ok("删除成功") ;
    }

    //回显
    @GetMapping("/{id}")
    public NewsType getById(@PathVariable Long id) {
        return newsTypeService.getById(id);
    }

    //修改
    @PutMapping
    public Result update(@RequestBody NewsType newsType) {
        newsTypeService.updateById(newsType);
        return Result.ok("修改成功");
    }

    @GetMapping("/p/{id}")
    public NewsType getNewsTypeById(@PathVariable Long id) {
        return newsTypeService.getById(id);
    }


}
